﻿using System.Security.Cryptography;
using System.Text;

namespace WorkTool2.Helpers;

public static class ToolHelper
{
    /// <summary>
    /// 拆分符号
    /// </summary>
    public const string SPLIT_STRING = "#####";

    public const string MARKDOWN_FOLDER = "markdown";

    public const string MARKDOWN_FILES_FOLDER = "files";

    /// <summary>
    /// 默认保存地址
    /// </summary>
    private static readonly string _saveFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WorkTool");

    /// <summary>
    /// 月份转换
    /// </summary>
    public static readonly Dictionary<string, string> _month = new()
    {
        {"01","Jan"},
        {"02","Feb"},
        { "03", "Mar"},
        { "04", "Apr"},
        { "05", "May"},
        { "06", "Jun"},
        { "07", "Jul"},
        { "08", "Aug"},
        { "09", "Sep"},
        { "10", "Oct"},
        { "11", "Nov"},
        { "12", "Dec"}
    };

    /// <summary>
    /// 创建文件夹
    /// </summary>
    /// <param name="saveFolder">文件保存地址</param>
    public static string CreateDirectory(string saveFolder)
    {
        if (string.IsNullOrWhiteSpace(saveFolder))
            saveFolder = _saveFolder;
        try
        {
            DirectoryInfo directory = Directory.CreateDirectory(saveFolder);
        }
        catch (Exception ex) when (ex is UnauthorizedAccessException ||
                                  ex is PathTooLongException ||
                                  ex is IOException)
        {
            throw;
        }
        return saveFolder;
    }

    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string CalculateMD5(this string input)
    {
        byte[] hashBytes = MD5.HashData(Encoding.UTF8.GetBytes(input));
        return string.Concat(hashBytes.Select(b => b.ToString("x2")));
    }

    /// <summary>
    /// URL编码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string EscapeDataString(this string input)
    {
        return Uri.EscapeDataString(input).Replace("%2F", "/"); ;
    }

    public static HashSet<string> _images = [".jpe", ".jpeg", ".png", ".gif", ".webp"];

    public static string HolidayNum(this string input)
    {
        return decimal.Parse(input).ToString("G999");
    }
}